#!/bin/bash

script_path=$(readlink -f "$0")
# 使用 dirname 获取脚本所在的目录
script_dir=$(dirname "$script_path")
echo "脚本所在目录: $script_dir"

# 安装 Flask（如果尚未安装）
if ! command -v flask &> /dev/null
then
    echo "Flask is not installed. Installing Flask..."
    sudo apt update
    sudo apt install -y python3-flask python3-pip
    # 如果使用 pip3 安装额外的 Flask 扩展，可以在这里添加
fi

# 给予 server.py 执行权限
chmod +x "$script_dir/run.sh"
# 创建桌面文件
desktop_file="/usr/share/applications/test_server.desktop"
sudo echo "[Desktop Entry]" > "$desktop_file"
sudo echo "Encoding=UTF-8" >> "$desktop_file"
sudo echo "Type=Application" >> "$desktop_file"
sudo echo "Categories=Network;" >> "$desktop_file"
sudo echo "Icon=$script_dir/test_server.png" >> "$desktop_file"
sudo echo "Exec=$script_dir/run.sh" >> "$desktop_file"
sudo echo "Name=测试服务器" >> "$desktop_file"
sudo echo "Comment=测试服务器 on python3" >> "$desktop_file"
sudo echo "Terminal=true" >> "$desktop_file"
sudo echo "Created desktop file: $desktop_file"

echo "install end ...."
